package slavik.voloshyn.vashmagazyn;

public class Announcement
{
	private final String price;
	private final String description;
	private String phone;
	private final String countRoom;
	private final String street;
	private final String floor;
	private final String area;
	private final String currency;
	private final CustomVector<String> keys;

	protected Announcement(String theCountRoom, String thePhone,
	        String thePrice, String theDescription, String theStreet,
	        String theFloor, String theArea, String theCurrency)
	{
		price = thePrice;
		description = theDescription;
		countRoom = theCountRoom;
		street = theStreet;
		floor = theFloor;
		area = theArea;
		currency = theCurrency;

		keys = new CustomVector<String>();
		phone = thePhone;
		phone = phone.replaceAll("-", "");
		phone = phone.replaceAll("\\(", "");
		phone = phone.replaceAll("\\)", "");
		addKeys();
	}

	private void addKeys()
	{
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < phone.length(); i++)
		{
			if (Character.isDigit(phone.charAt(i)))
			{
				stringBuffer.append(phone.charAt(i));
			}
			else if (stringBuffer.length() > 4 && stringBuffer.length() <= 15)
			{
				keys.add(stringBuffer.toString());
				// System.out.println(stringBuffer);
				stringBuffer.delete(0, stringBuffer.length());
			}
			else if (stringBuffer.length() <= 4)
			{
				stringBuffer.delete(0, stringBuffer.length());
			}
		}
	}

	public CustomVector<String> getKeys()
	{
		return keys;
	}

	@Override
	public String toString()
	{
		StringBuffer announcementText = new StringBuffer();
		announcementText.append("Street:").append(street).append("\n");
		announcementText.append("Rooms:").append(countRoom).append("\n");
		announcementText.append("Floor:").append(floor).append("\n");
		announcementText.append("Area:").append(area).append("\n");
		announcementText.append("Price:").append(price).append(currency)
		        .append("\n");
		announcementText.append("Description:").append(description)
		        .append("\n");
		announcementText.append("Phone:").append(phone).append("\n");
		return announcementText.toString();
	}

}
